package pl.jtb.ctl.tabory.ui.editors;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import pl.jtb.ctl.tabory.ui.osgi.UiBundleActivator;
import pl.jtb.ctl.tabory.ui.selection.ISelection;
import pl.jtb.ctl.tabory.ui.selection.ISelectionListener;
import pl.jtb.ctl.tabory.ui.selection.ISelectionProvider;

public abstract class AbstractMasterSectionPart<T extends ISelection> extends SectionPart implements ISelectionProvider<T> {

    protected T selection;
    protected String sectionName;
    protected Section parentSection;
    protected FormToolkit toolkit;

    protected Table table;
    protected TableViewer tableViewer;

    protected ToolBar toolBar;
    protected ToolItem toolItemNew;
    protected ToolItem toolItemEdit;
    protected ToolItem toolItemDelete;

    protected Set<ISelectionListener<T>> selectionListeners;

    public AbstractMasterSectionPart(Composite parent, FormToolkit toolkit, int style, String sectionName) {
        super(parent, toolkit, style);
        this.toolkit = toolkit;
        this.sectionName = sectionName;
        selectionListeners = new HashSet<ISelectionListener<T>>();
        parentSection = getSection();
        parentSection.setText(sectionName);
    }

    protected abstract void initGui(Section parentSection, FormToolkit toolkit);

    protected abstract void postInitGui(Section parentSection, FormToolkit toolkit);

    protected abstract void initActions();

    protected abstract void refreshToolBar();

    protected void setEnabled(boolean isEnabled) {
        if (table != null) {
            table.setEnabled(isEnabled);
        }
        if (tableViewer != null) {
            toolBar.setEnabled(isEnabled);
        }
    }

    protected ToolBar getToolBar(Section parentSection, FormToolkit toolkit) {
        ToolBar toolBar = new ToolBar(parentSection, SWT.FLAT | SWT.HORIZONTAL);
        toolItemNew = new ToolItem(toolBar, SWT.FLAT);
        toolItemNew.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_NEW_WIZARD));
        toolItemNew.setToolTipText(UiBundleActivator.getMessage("common.addnew"));
        toolItemEdit = new ToolItem(toolBar, SWT.PUSH);
        toolItemEdit.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_COPY));
        toolItemEdit.setToolTipText(UiBundleActivator.getMessage("common.edit"));
        toolItemDelete = new ToolItem(toolBar, SWT.PUSH);
        toolItemDelete.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE));
        toolItemDelete.setToolTipText(UiBundleActivator.getMessage("common.delete"));
        toolkit.paintBordersFor(toolBar);
        return toolBar;
    }

    public Table getTable() {
        return table;
    }

    public TableViewer getTableViewer() {
        return tableViewer;
    }

}
